package com.ruoyi.project.wms.customerorder.controller;

import java.util.List;

import com.ruoyi.project.wms.essentials.purchase.service.IWmsPurchaseService;
import com.ruoyi.project.wms.space.domain.WmsStroeSpace;
import com.ruoyi.project.wms.space.service.IWmsStroeSpaceService;
import com.ruoyi.project.wms.status.domain.WmsCustomerStatus;
import com.ruoyi.project.wms.status.service.IWmsCustomerStatusService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.wms.customerorder.domain.WmsCustomerOrder;
import com.ruoyi.project.wms.customerorder.service.IWmsCustomerOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * 出库订单信息表Controller
 * 
 * @author ruoyi
 * @date 2020-10-29
 */
@Controller
@RequestMapping("/wms/customerorder")
public class WmsCustomerOrderController extends BaseController
{
    private String prefix = "wms/customerorder";

    @Autowired
    private IWmsCustomerOrderService wmsCustomerOrderService;

    @Autowired
    private IWmsPurchaseService wmsPurchaseService;
    @RequiresPermissions("wms:customerorder:view")
    @GetMapping()
    public String customerorder()
    {
        return prefix + "/customerorder";
    }

    /**
     * 查询出库订单信息表列表
     */
    @RequiresPermissions("wms:customerorder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsCustomerOrder wmsCustomerOrder)
    {
        startPage();
        List<WmsCustomerOrder> list = wmsCustomerOrderService.selectWmsCustomerOrderList(wmsCustomerOrder);
        return getDataTable(list);
    }


    /**
     * 导出出库订单信息表列表
     */
    @RequiresPermissions("wms:customerorder:export")
    @Log(title = "出库订单信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsCustomerOrder wmsCustomerOrder)
    {
        List<WmsCustomerOrder> list = wmsCustomerOrderService.selectWmsCustomerOrderList(wmsCustomerOrder);
        ExcelUtil<WmsCustomerOrder> util = new ExcelUtil<WmsCustomerOrder>(WmsCustomerOrder.class);
        return util.exportExcel(list, "customerorder");
    }

    /**
     * 新增出库订单信息表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存出库订单信息表
     */
    @RequiresPermissions("wms:customerorder:add")
    @Log(title = "出库订单信息表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsCustomerOrder wmsCustomerOrder, HttpServletRequest req)
    {
        System.out.println(wmsCustomerOrder.toString()+"*************************************************************************************");
//        List<WmsCustomerOrder> list = wmsCustomerOrderService.selectWmsCustomerOrderList(wmsCustomerOrder);
        Integer customerOrderNumber1 = wmsCustomerOrder.getCustomerOrderNumber();//获取传过来的订单的数量
        Integer pid = wmsCustomerOrder.getpId();//当前商品id
        WmsStroeSpace wmsStroeSpace = wmsCustomerOrderService.selectWmsStroeSpaceByPid(pid);//当前商品库存类
        int stroeSpaceNum = wmsStroeSpace.getStroeSpaceNum().intValue();//当前商品库存
        if (customerOrderNumber1<=stroeSpaceNum){
            return toAjax(wmsCustomerOrderService.insertWmsCustomerOrder(wmsCustomerOrder));
        }else {
            return toAjax(false);
        }


    }

    /**
     * 修改出库订单信息表
     */
    @GetMapping("/edit/{customerOrderId}")
    public String edit(@PathVariable("customerOrderId") Integer customerOrderId, ModelMap mmap)
    {
        WmsCustomerOrder wmsCustomerOrder = wmsCustomerOrderService.selectWmsCustomerOrderById(customerOrderId);
        mmap.put("wmsCustomerOrder", wmsCustomerOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存出库订单信息表
     */
    @RequiresPermissions("wms:customerorder:edit")
    @Log(title = "出库订单信息表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsCustomerOrder wmsCustomerOrder)
    {
        return toAjax(wmsCustomerOrderService.updateWmsCustomerOrder(wmsCustomerOrder));
    }

    /**
     * 删除出库订单信息表
     */
    @RequiresPermissions("wms:customerorder:remove")
    @Log(title = "出库订单信息表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsCustomerOrderService.deleteWmsCustomerOrderByIds(ids));
    }
}
